package com.wjjun.base.dagger.module;

import android.content.Context;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.cocosw.favor.FavorAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.litesuits.orm.LiteOrm;
import com.qingmi.farmer.data.DataSetting;
import com.qingmi.farmer.data.UserData;
import com.wjjun.base.data.cache.CacheProviders;
import com.wjjun.base.data.db.DBManager;
import com.wjjun.base.data.db.IDBManager;
import com.wjjun.base.data.net.TokenFilter;
import com.wjjun.base.data.preference.UserPreference;
import com.wjjun.base.App;
import com.wjjun.basemvpvm.data.executor.JobExecutor;
import com.wjjun.basemvpvm.data.executor.PostExecutionThread;
import com.wjjun.basemvpvm.data.executor.ThreadExecutor;
import com.wjjun.basemvpvm.data.executor.UIThread;
import com.wjjun.basemvpvm.data.net.HttpApiManager;
import com.wjjun.basemvpvm.data.net.filter.BaseResponseFilter;
import com.wjjun.utils.AppUtils;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import io.rx_cache2.internal.RxCache;
import io.victoralbertos.jolyglot.GsonSpeaker;

/**
 * appModule
 * 这里提供单例化的对象
 * Created by 67535 on 2018/5/25.
 */
@Module
public class AppModule {

    private App app;

    public AppModule(App app) {
        this.app = app;
    }

    @Provides
    @Singleton
    Context provideApplicationContext() {
        return this.app;
    }

    @Provides
    @Singleton
    ThreadExecutor provideThreadExecutor(JobExecutor jobExecutor) {
        return jobExecutor;
    }

    @Provides
    @Singleton
    PostExecutionThread providePostExecutionThread(UIThread uiThread) {
        return uiThread;
    }

    @Provides
    @Singleton
    LiteOrm provideLiteOrm() {
        return LiteOrm.newSingleInstance(app, "DB");
    }

    @Provides
    @Singleton
    IDBManager provideDBManager(DBManager dbManager) {
        return dbManager;
    }

    @Provides
    @Singleton
    UserPreference providesUserPreference() {
        return new FavorAdapter.Builder(app).build().create(UserPreference.class);
    }

    @Provides
    @Singleton
    BaseResponseFilter provideBaseResponseFilter() {
        return new TokenFilter(app);
    }

    @Provides
    @Singleton
    HttpApiManager providesHttpApiManager() {
        return HttpApiManager.getInstance().init(DataSetting.SERVER_HOST, DataSetting.SERVER_PORT,
                DataSetting.SERVER_PATH);
    }

    @Provides
    @Singleton
    Gson provideGson() {
        return new GsonBuilder().serializeNulls().create();
    }

    @Provides
    @Singleton
    UserData provideUserData(UserPreference userPreference) {
        return UserData.getInstance().init(userPreference);
    }

    @Provides
    @Singleton
    CacheProviders providerCacheProviders() {
        return new RxCache.Builder().useExpiredDataIfLoaderNotAvailable(true)
                .persistence(AppUtils.getCacheDir(app), new GsonSpeaker())
                .using(CacheProviders.class);
    }

    @Provides
    @Singleton
    AMapLocationClient providerAMapLocationClient() {
        AMapLocationClient aMapLocationClient = new AMapLocationClient(app);
        AMapLocationClientOption option = new AMapLocationClientOption();
        option.setNeedAddress(true);//设置是否返回地址信息
        option.setInterval(100000L);//时间间隔10s
        aMapLocationClient.setLocationOption(option);
        return aMapLocationClient;
    }
}
